package com.oren.user.Test.activity;



import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricActivityInstanceQuery;
import org.activiti.engine.history.HistoricDetail;
import org.activiti.engine.history.HistoricDetailQuery;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;

/**
 * Created by IntelliJ IDEA.
 * User: wangshiwei
 * Date: 2018/7/26
 * Time: 9:51
 * describe:
 */

public class ActivityTest extends BaseJUnit{

    //创建表
    @Test
    public void createTable(){
        // 创建流程引擎配置信息对象
        ProcessEngineConfiguration pec = ProcessEngineConfiguration
                .createStandaloneProcessEngineConfiguration();
        // 设置数据库的类型
        pec.setDatabaseType("mysql");
        // 设置创建数据库的方式
        // ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE(true): 如果没有数据库表就会创建数据库表，有的话就修改表结构.
        // ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE(false): 不会创建数据库表
        // ProcessEngineConfiguration.DB_SCHEMA_UPDATE_CREATE_DROP(create-drop): 先创建、再删除.
        pec.setDatabaseSchemaUpdate("true");
        // 设置数据库驱动
        pec.setJdbcDriver("com.mysql.jdbc.Driver");
        // 设置jdbcURL
        pec.setJdbcUrl("jdbc:mysql://localhost:3306/sys");
        // 设置用户名
        pec.setJdbcUsername("root");
        // 设置密码
        pec.setJdbcPassword("123456");
        // 构建流程引擎对象
        ProcessEngine pe = pec.buildProcessEngine(); // 调用访方法才会创建数据表
        // 调用close方法时，才会删除
        pe.close();
    }

    //部署流程
    @Test
    public void TestDeploy(){
        //FileInputStream fileInputStream=new FileInputStream();
        //ZipInputStream zipInputStream=new ZipInputStream();

        Deployment deployment=processEngine.getRepositoryService()
                .createDeployment()
                .name("test")
                .addClasspathResource("bpmn/aa.bpmn")
                .deploy();
        System.out.println(deployment.getId());
        System.out.println(deployment.getName());


    }

    //查询流程
    @Test
    public void queryDeploy(){
        ProcessDefinition pd =processEngine.getRepositoryService().createProcessDefinitionQuery()
                .deploymentId("22501").singleResult();
        System.out.println("key:"+pd.getKey());
        System.out.println("id:"+pd.getId());



    }

    //启动流程
    @Test
    public void start(){
        RuntimeService runtimeService = processEngine.getRuntimeService();
        Map<String, Object> variables = new HashMap<String,Object>();
      //  variables.put("userName","张三");
        runtimeService.startProcessInstanceById("oren:1:22504");
                //runtimeService.startProcessInstanceByKey("qingjia");
    }


    //查询流程当前节点信息
    public void queryByName(String name){
        TaskService taskService=processEngine.getTaskService();
        System.out.println("=========="+name+"未审核的================");
        List<Task> taskList=taskService.createTaskQuery().taskCandidateOrAssigned(name).list();

        for (Task task:taskList) {
            String pdid=task.getProcessDefinitionId();
            System.out.println("==============");
            System.out.println("ID:"+task.getId()+",姓名:"+task.getName()+",接收人:"+task.getAssignee()+",开始时间:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(task.getCreateTime()));
        }
    }

    @Test
    public void Test(){
        queryByName("李四");
      // completeTask("25004");
    }

    public void completeTask(String id){
        Map<String, Object> variables = new HashMap<String,Object>();
        variables.put("Name","王二麻子");
       TaskService taskService= processEngine.getTaskService();
       //taskService.setVariables(id,variables);
       taskService.complete(id);
    }


    @Test
    public  void queryHistory(){
       /* List<HistoricActivityInstance> historicDetailQuery=processEngine.getHistoryService()
                .createHistoricActivityInstanceQuery().activityWithoutTenantId().executionId("5").list();
        for (HistoricActivityInstance historicDetail:historicDetailQuery) {
            System.out.println(historicDetail.getId()+"   "+ historicDetail.getTaskId()+" " +historicDetail.getExecutionId()+" "+ historicDetail.getProcessInstanceId());
        }*/
        TaskService taskService=processEngine.getTaskService();
        Task task1 = taskService.createTaskQuery().executionId("15").singleResult();
        System.out.println(task1.getAssignee()+"          "+task1.getId());
    }
}
